/****************************************************************************
**
** This file is part of qPhotoTag, a photo keywording application
**
** Copyright (C) 2009 Marcell Lengyel <miketkf@gmail.com>
**
** GNU General Public License
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
****************************************************************************/
#include "wptlistdialog.h"
#include "ui_wptlistdialog.h"


WptListDialog::WptListDialog(QFile &xmlFile, QWidget *parent)
    : QDialog(parent), ui(new Ui::WptListDialogClass)
{
    sel.lat = 0;
    sel.lon = 0;
    sel.altitude = 0;
    sel.name.clear();

    ui->setupUi(this);
    //QWidget::setWindowFlags(Qt::FramelessWindowHint);
    connect(ui->selectButton, SIGNAL(clicked()), this,
                SLOT(updateSelected()));

    QXmlInputSource source( &xmlFile );
    QXmlSimpleReader reader;
    GpxWptHandler handler(waypoint_list);
    reader.setContentHandler(&handler);
    reader.setErrorHandler(&handler);
    reader.parse(source);
    for (int i = 0; i < waypoint_list.size(); ++i) {
        ui->listWidget->addItem(waypoint_list[i]->name);
    }

}

WptListDialog::~WptListDialog()
{
    delete ui;
    while (!waypoint_list.isEmpty())
        delete waypoint_list.takeFirst();
}

void WptListDialog::updateSelected()
{
    for (int i = 0; i < waypoint_list.size(); ++i) {
        if (ui->listWidget->currentItem()->text() == waypoint_list[i]->name) {
            sel = *waypoint_list[i];
            QDialog::close();
        }
    }
}

GpxWpt WptListDialog::selectedWaypoint()
{
    return sel;
}
